<template>
  <div id="daily" style="background: #fff; position: relative">
    <div class="num">
      <span style=""> {{ index + 1 }} </span> {{ "/" + questionList.length }}
    </div>
    <div class="questionsList">
      <radioT
        v-if="item.type == '单选题'"
        :item="item"
        @changeres="changeres"
        :index="0"
        :isdetail="item.enabledMark != 0"
      >
      </radioT>

      <tiankong
        v-if="item.type == '填空题'"
        :item="item"
        :index="0"
        :isdetail="item.enabledMark != 0"
      ></tiankong>
      <radiod
        v-if="item.type == '多选题'"
        :item="item"
        @changeres="changeres"
        :index="0"
        :isdetail="item.enabledMark != 0"
      >
      </radiod>

      <determine
        v-if="item.type == '判断题'"
        :item="item"
        @changeres="changeres"
        :index="0"
        :isdetail="item.enabledMark != 0"
      >
      </determine>
      <answers
        v-if="item.type == '问答题'"
        :item="item"
        @changeres="changeres"
        :index="0"
        :isdetail="item.enabledMark != 0"
      >
      </answers>
    </div>
    <div class="btn" style="margin: auto">
      <el-button type="primary" @click="submitBtn" v-if="!isOver">
        确认
      </el-button>
      <el-button
        type="primary"
        @click="nextti"
        v-if="isOver && questionList.length != index + 1"
      >
        下一题
      </el-button>

      <el-button
        type="primary"
        @click="back"
        v-if="isOver && questionList.length == index + 1"
      >
        结束返回
      </el-button>
    </div>
  </div>
</template>

<script>
import radioT from "./components/radio";
import tiankong from "./components/tiankong";
import request from "@/utils/request";
import radiod from "./components/radiod";

import determine from "./components/determine";
import answers from "./components/answers";
import {
  saveInterestQuestionResult, //兴趣
  saveCompleteTheAnswer,
} from "@/views/exam/api/temsExamStudy";
export default {
  name: "certificate",
  components: {
    radioT,
    tiankong,
    radiod,
    determine,
    answers,
  },
  props: {
    questionList: {
      type: Array,
      default: [],
    },
  },
  data() {
    return {
      isdetail: false,
      isOver: false,
      index: 0,
      item: {}, // 当前题目对象
    };
  },
  mounted() {},
  methods: {
    back() {
      this.$emit("back");
    },
    init(index) {
      if (index) this.index = index;
      this.item = this.questionList[this.index];
    },
    nextti() {
      this.isOver = false;
      this.index = this.index + 1;
      this.item = {};
      this.$nextTick(() => {
        this.item = this.questionList[this.index];
      });
    },
    submitBtn() {
      var objItem = { ...this.item };

      if (
        objItem.questionType == "selectTi" ||
        objItem.questionType == "multiple" ||
        objItem.questionType == "determine"
      ) {
        objItem.options = JSON.stringify(ele.options);
      }
      if (this.index + 1 == this.questionList.length) {
        saveCompleteTheAnswer(objItem).then((res) => {
          this.questionList = res.data.questionList;
          this.item = this.questionList[this.index];
          // this.item.options = JSON.parse(this.item.options)
          this.changeList(this.questionList);
          this.isOver = true;
        });
      } else {
        saveInterestQuestionResult(objItem).then((res) => {
          this.questionList[this.index] = res.data;
          this.item = this.questionList[this.index];
          // this.item.options = JSON.parse(this.item.options)
          this.changeList([this.item]);
          this.isOver = true;
        });
      }
    },
    changeList(arr) {
      arr.forEach((el) => {
        if (el.questionType == "selectTi") {
          el.type = "单选题";
          el.options = JSON.parse(el.options);
        } else if (el.questionType == "multiple") {
          el.type = "多选题";
          el.options = JSON.parse(el.options);
        } else if (el.questionType == "determine") {
          el.type = "判断题";
          el.options = JSON.parse(el.options);
        } else if (el.questionType == "blank") {
          el.type = "填空题";
        } else if (el.questionType == "answers") {
          el.type = "问答题";
        }
      });
    },
  },
  beforeDestroy() {},
};
</script>

<style lang="scss" scoped>
.num {
  position: absolute;
  top: 0px;
  right: 30px;

  span {
    font-size: 18px;
    font-weight: 600;
    color: #2a8ff7;
  }
}

.btn {
  width: 80px;
}

#daily {
  // padding: 20px;
  float: right;
  margin-top: 20px;
  width: 955px;
  background-color: #fff;
}
</style>
